/*
 * Created on Dec 5, 2003
 * 
 * To change the template for this generated file go to Window - Preferences -
 * Java - Code Generation - Code and Comments
 */
package org.vectrics.recruiter.struts.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.vectrics.common.db.SimpleCodedObject;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.struts.VectricsActionForm;

public class LocationForm extends VectricsActionForm {
    public static final long serialVersionUID = 89992352;

    private Logger log = Logger.getLogger(this.getClass());

    private String id;

    private String parentName;

    private String parentId;

    private String name;

    private String abbreviation;

    private String description;

    private String typeCode;

    /**
	 * 
	 */
    public LocationForm() {
        super();
    }

    @Override
    public ActionErrors doValidate(final ActionMapping mapping, final Request request) {
        log.debug("validate() called");
        ActionErrors errors = new ActionErrors();
        if (wasButtonPressed(request, "cancel") == false) {
            if (getName() == null || getName().trim().length() <= 0) {
                errors.add("name", new ActionMessage("error.required.location"));
            }

        } else {
            log.debug("Cancel button was pressed, no validation performed");
        }

        if (errors.isEmpty()) {
            log.debug("validate() - no errors");
        } else {
            log.debug("validate() - errors found");
        }

        boolean isCountry = getTypeCode() != null ? getTypeCode().equalsIgnoreCase("country") : false;
        LocationForm.addLocationTypeCollection(request.getLocale(), request, isCountry);

        String mappingAttr = mapping.getAttribute();
        request.setAttribute(mappingAttr, this);
        return errors;
    }

    /**
     * @return
     */
    public String getId() {
        return id;
    }

    /**
     * @param code
     */
    public void setId(final String id) {
        this.id = id;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(final String parentId) {
        this.parentId = parentId;
    }

    public String getParentName() {
        return parentName;
    }

    public void setParentName(final String parentName) {
        this.parentName = parentName;
    }

    /**
     * @return
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description
     */
    public void setDescription(final String description) {
        this.description = description;
    }

    /**
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     */
    public void setName(final String name) {
        this.name = name;
    }

    public String getTypeCode() {
        return typeCode;
    }

    public void setTypeCode(final String typeCode) {
        this.typeCode = typeCode;
    }

    /**
     * @return the abbreviation
     */
    public String getAbbreviation() {
        return abbreviation;
    }

    /**
     * @param abbreviation
     *            the abbreviation to set
     */
    public void setAbbreviation(final String abbreviation) {
        this.abbreviation = abbreviation;
    }

    /**
     * Save a list of Location types to a request attribute, to be used by edit
     * form to populate a combo box.
     * 
     * @param locale
     * @param request
     */
    public static void addLocationTypeCollection(final Locale locale, final Request request, final boolean isCountry) {
        List locationTypes = new ArrayList();

        if (isCountry) {
            SimpleCodedObject locationType = new SimpleCodedObject();
            locationType.setCode("country");
            locationType.setName(CoreServiceLocator.getPhraseProvider().getMessage("location.type.country", locale));
            locationTypes.add(locationType);
        }

        SimpleCodedObject locationType = new SimpleCodedObject();
        locationType.setCode("state");
        locationType.setName(CoreServiceLocator.getPhraseProvider().getMessage("location.type.state", locale));
        locationTypes.add(locationType);

        locationType = new SimpleCodedObject();
        locationType.setCode("city");
        locationType.setName(CoreServiceLocator.getPhraseProvider().getMessage("location.type.city", locale));
        locationTypes.add(locationType);

        request.setAttribute("locationTypes", locationTypes);
    }

}
